package com.dede.locallib;

import java.lang.reflect.Method;
import java.util.HashMap;

/**
 * @author hsh
 * @date 2018/2/1 14:29.
 * @doc
 */
public class ReflectUtil {

    private static HashMap<String, Class<?>> sClasss;
    private static HashMap<String, Method> sMethods;

    static {
        sClasss = new HashMap<>();
        sMethods = new HashMap<>();
    }

    public static Class<?> clazz(String className) throws ClassNotFoundException {
        Class<?> clazz = sClasss.get(className);
        if (clazz == null) {
            clazz = Class.forName(className);
            sClasss.put(className, clazz);
        }
        return clazz;
    }

    public static Method method(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws NoSuchMethodException {
        int length = parameterTypes.length;
        String key = clazz.getName()
                .concat(methodName)
                .concat(String.valueOf(length));
        Method method = sMethods.get(key);
        if (method == null) {
            method = clazz.getMethod(methodName, parameterTypes);
            sMethods.put(key,method);
        }
        return method;
    }
}
